package com.dcd.service.impl;

import com.dcd.mapper.UserMapper;
import com.dcd.pojo.User;
import com.dcd.pojo.Video;
import com.dcd.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @param user 用户对象
     * @return
     */
    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    /**
     * @param user 用户对象
     * @return
     */
    @Override
    public boolean register(User user) {
        return userMapper.register(user) == 1;
    }

    /**
     * @param user_phone 手机号
     * @return
     */
    @Override
    public boolean select_phone_whether_register(String user_phone) {
        return userMapper.select_phone_whether_register(user_phone) == 1;
    }

    /**
     * 查询微信登录用户是否被添加到数据库
     *
     * @param user_name 用户名
     * @return
     */
    @Override
    public boolean select_we_chat_whether_add(String user_name) {
        return userMapper.select_we_chat_whether_add(user_name) > 0;
    }

    /**
     * 添加微信登录用户对象
     *
     * @param user 用户对象
     * @return
     */
    @Override
    public Map<String, Object> add_we_chat_user(User user) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("result", userMapper.add_we_chat_user(user));
        hashMap.put("user", user);
        return hashMap;
        //return userMapper.add_we_chat_user(user) == 1;
    }

    /**
     * 根据指定用户名查询对象
     *
     * @param user_name 用户名
     * @return
     */
    @Override
    public User select_user_by_user_name(String user_name) {
        return userMapper.select_user_by_user_name(user_name);
    }

    /**
     * 根据用户id查询所有发布的视频
     * @param user_id   用户id
     * @return
     */
    @Override
    public Map<String,Object> select_videoList_byUserId(Integer user_id, Integer pageindex) {
        Map<String,Object> map=new HashMap<String, Object>();
        Page<Video> staPage= PageHelper.startPage(pageindex,2);
        userMapper.select_videoList_byUserId(user_id);
        map.put("pageIndex",staPage.getPageNum());//当前页
        map.put("totalPage",staPage.getPages()); //总页数
        map.put("VideoList",staPage.getResult()); //数据
        return map;
    }

    /**
     *  根据用户id查询所有发布的资讯
     * @param user_id   用户id
     * @return
     */
    @Override
    public Map<String, Object> select_information_byUserId(Integer user_id, Integer pageIndex) {
        Map<String,Object> map=new HashMap<String, Object>();
        Page<Video> staPage= PageHelper.startPage(pageIndex,2);
        userMapper.select_information_byUserId(user_id);
        map.put("pageIndexs",staPage.getPageNum());//当前页
        map.put("totalPages",staPage.getPages()); //总页数
        map.put("InformationLists",staPage.getResult()); //数据
        return map;
    }

    @Override
    public int existsFenSi(Integer user_id, Integer cover_user_id) {
        return userMapper.existsFenSi(user_id,cover_user_id);
    }

    @Override
    public int existsSelect_videoList_byUserId(Integer user_id) {
        return userMapper.existsSelect_videoList_byUserId(user_id);
    }

    @Override
    public int existsSelect_information_byUserId(Integer user_id) {
        return userMapper.existsSelect_information_byUserId(user_id);
    }
}
